/** @jsxImportSource @emotion/react */
import * as React from "react";
import {
    Button, TextArea, Text, useToast,
} from "customize-easy-ui-component";
import {InspectRecordLayout, InternalItemProps, useItemInputControl} from "../../common/base";
import {useMedia} from "use-media";
import {assertNamesUnique} from "../../common/eHelper";
import {EditStorageContext} from "../../StorageContext";
import {itemA下结论} from "./editor";
import {config测试工况, itemA试工况} from "./TestInstruction";
import {itemA仪表} from "./InstrumentDesc";
import {itemA数综合} from "./Comprehensive";
import {itemA原因分, itemA它问题, itemA异常指} from "./SituationSuggest";


const 出口介选=['水蒸汽','饱和水蒸汽'];
export const config设备概况 = [
    [['锅炉型号', '_$型号'], ['使用登记证编号', '_$使用证号'], ],
    [['锅炉名称', '_$设备名称'], ['锅炉产品编号', '_$出厂编号'], ],
    [['锅炉出口介质', {n:'出口介',t:'l',l:出口介选},], ['额定出力', '_$额定出力','t/h'],  ['额定压力', '_$额定压力','MPa'] ],
    //原模版 710006 锅炉能效测试综合报告.pdf 的“口”字奇怪？可选择打勾：
    [['出口介质温度', '_$介质出口温度','°C'],  ['燃烧方式', '_$燃烧方式'],  ['设计燃料', '_$燃料种类',] ],
    [['进口介质温度', {n:'进介温',u:'°C'},],  ['燃烧设备', '_$燃烧设备'],  ['使用燃料', '用燃料',] ],
    [['尾部受热面', {n:'尾受热',t:'B'},],  ],
    //拆分点
    [['制造单位名称', '_$制造单位'], ['联系人','_$设备联系人'], ],
    //isp?。服务单位
    [['委托单位名称', '_$服务单位'], ['联系电话','_$设备联系手机'], ],
    [['使用单位名称', '_$使用单位'], ['测试日期','_$检验日期'], ],
    [['测试地点', '_$设备使用地点'],  ],
];

export const EntranceSetup =
    React.forwardRef((
        {children, show, alone = true, repId, verId, rep}: InternalItemProps, ref
    ) => {
        const atPrint = useMedia('print');
        const toast = useToast();
        const doCheckNames = React.useCallback((verId: string) => {
            const result = assertNamesUnique([{value: rep?.tzFields},
                {value: config设备概况, type:'esnt'},
                {value:[...itemA下结论, ...itemA试工况]},
                {value: config测试工况, type:'esnt'},
                {value:[...itemA仪表, ...itemA数综合, ...itemA异常指, ...itemA原因分, ...itemA它问题 ]},
                {value:['Projects', ]}
                ]);
            toast({title: "完成！", subtitle: result ? "没发现冲突" : "测试开关没开", intent: "success"});
        }, [rep,toast]);
        const {storage, setStorage} =React.useContext(EditStorageContext) as any;
        const getInpFilter = React.useCallback((par: any) => {
            const {_tblFixed, } =par||{};
            return {_tblFixed, };
        }, []);
        const {inp, setInp} = useItemInputControl({ref});
        if (atPrint) return null;
        else return (
            <InspectRecordLayout inp={inp} setInp={setInp} getInpFilter={getInpFilter} show={show}
                                 alone={alone} label={'初始化本报告，默认值配置等'}>
                {process.env.REACT_APP_TEST==='true' && <><div>
                    <Text variant="h5">构建开发模板时的工具：校验模板的存储name冲突；</Text>
                    <Button intent='primary' onPress={() => doCheckNames(verId!)}>校验模板name唯一性</Button>
                    <Text variant="h5">设置待测试表格的各列宽度：</Text>
                    <TextArea value={storage?._tblFixed || ''} rows={2} onChange={e =>{
                        JSON.parse(e.currentTarget.value??'[]');
                        setStorage({ ...storage, _tblFixed: e.currentTarget.value || undefined}); }}/>
                </div></>
                }
                <hr/>
            </InspectRecordLayout>
        );
    });
